home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Communication / Weather / Source / License.m < prev    next >
Text File  |  1993-11-14  |  8KB  |  279 lines

  1. /*
  2.     Copyright (c) 1991, 1992 by the MIT Media Laboratory
  3.     
  4.     This software is distributed by Michael Hawley of the MIT Media Laboratory. 
  5.     We hope it will be useful to you.
  6.     
  7.     Permission to use, copy, or modify this software for educational 
  8.     and research purposes only and without fee is hereby granted 
  9.     provided this notice appears on all copies, and provided you send 
  10.     us your improvements.  Any other use of this software, in original 
  11.     or modified form, in whole or in part, requires specific permission 
  12.     from MIT.  This software shall not be used, rewritten, or adapted 
  13.     for use in a commercial product without first obtaining appropriate 
  14.     licenses from MIT.  MIT makes no representations about the suitability 
  15.     of this software for any purpose: it is provided "as is" without any 
  16.     warranty and any risk, damage, or liability incurred through your use 
  17.     of this software is yours alone.
  18.     
  19.     Michael Hawley
  20.     MIT Media Laboratory
  21.     20 Ames Street, 
  22.     Cambridge, MA 02139
  23.     mike@media-lab.mit.edu
  24. */
  25. /*
  26.  *
  27.  * Implement the License & info panels, receipt mechanism, and Suggestion box.
  28.  *
  29.  * The License declares the Media Laboratory copyright with an
  30.  * enjoinder to prevent people from re-selling the software,
  31.  * but a hearty encouragement to urge folks to modify and improve
  32.  * the code -- providing the improvements are sent back to us
  33.  * and the license remains intact.  The License also exempts the
  34.  * Media Lab from any blame due to any damage incurred by users.
  35.  *
  36.  * Acceptance of the License (by first-time users) sends a receipt
  37.  * (akin to a "read receipt" in Mail) back to the home base,
  38.  * to maintain a census of the population using the code.
  39.  *
  40.  * Note that the first time this was tried without mention of the
  41.  * outgoing receipt, many reacted badly to the implication that
  42.  * software might act "behind their back."  But an acknowledged
  43.  * receipt should not be problematic, and it is not unreasonable
  44.  * for the author to want to understand the user community.
  45.  *
  46.  * The other feedback channel here is the -suggestion: method,
  47.  * which composes an e-mail letter so user's comments, bugs, and
  48.  * improvements can be collected.
  49.  *
  50.  *    IMPORTANT: #define ReceiptsTo and CommentsTo, below, and prepare
  51.  *        the receiving mailboxes.
  52.  *    
  53.  *
  54.  * Michael Hawley
  55.  * mike@media-lab.mit.edu
  56.  */
  57.  
  58. #import "Defaults.h"
  59. #import "License.h"
  60. #import "ColorBars.h"
  61. #import "ColorScroller.h"
  62.  
  63. @implementation License
  64.  
  65.         /* NULL or "" will disable receipt mechanism */
  66. #define ReceiptsTo    "receipts@tome.media.mit.edu"
  67. #define CommentsTo    "comments@tome.media.mit.edu"
  68.  
  69. #define AppName [NXApp appName]
  70. extern char *Version();
  71.  
  72. static char *local_stripnl(char *s){ /* remove trailing space */
  73.     char *p;
  74.     for (p=s;*p;p++) if (*p == '\n' || *p == '\r') *p = '\0';
  75.     return s;
  76. }
  77.  
  78. char *
  79. execstr(char *s) { /* replace contents of 's' with piped output */
  80.     FILE *f = popen(s,"r");
  81.     char *p = s;
  82.     *s = '\0';
  83.     if (f){
  84.         while (fgets(p,256,f))
  85.             local_stripnl(p), p += strlen(p);
  86.         pclose(f);
  87.     }
  88.     return s;
  89. }
  90.  
  91. - windowWillClose:sender {
  92.     [infobars stopIt];
  93.     return self;
  94. }
  95.  
  96. static id _version;
  97.  
  98. char *
  99. Version(void){
  100.     char *s = (char *)[_version stringValue];
  101.     return s? s : "1.0";
  102. }
  103.  
  104. int
  105. FirstUse(void){ // true if this is the first time the app's been used
  106.     return (*FirstUsed)? 0 : 1;
  107. }
  108.  
  109. void
  110. mailReceipt(char *subj, char *body, char *to)
  111. /*
  112.  * Send a read/run receipt form to "ReceiptsTo" address,
  113.  * to indicate that the user has accepted the License.
  114.  * If "to" and "ReceiptsTo" are blank (or NULL), this is a no-op.
  115.  */
  116. {
  117.     char s[1024], u[256]="whoami", m[256]="hostname", t[4096], tmp[128];
  118.     FILE *f;
  119.  
  120.     if (!to || !*to)     to   = ReceiptsTo;
  121.     if (!to || !*to)     return;
  122.     if (!subj || !*subj) subj = (char *)AppName;
  123.     execstr(u); execstr(m);
  124.     if (!body || !*body){
  125.         body = t;
  126.         sprintf(body, "\nIf this receipt bounces back to you,\n\
  127. could you please correct the address if possible,\n\
  128. and aim it to: %s\n\
  129. to let us know you're registered.\n",ReceiptsTo);
  130.     }
  131.     strcpy(tmp,"/tmp/openerXXXXXX"); mktemp(tmp);
  132.     if (f = fopen(tmp,"w")){
  133.         fprintf(f,"%s",body);
  134.         fclose(f);
  135.         sprintf(s,"(/usr/ucb/Mail -s \"%s %s@%s\" %s < %s; rm %s) &",
  136.                 subj,u,m,to,tmp,tmp);
  137.         system(s);
  138.     }
  139. }
  140.  
  141. - suggestion:sender {
  142.     char subj[256], w[256] = "whoami";
  143.     char body[4096]="\
  144. Michael:\n\n\
  145. Wow!  Sure beats watching the Weather Channel on cable!\n\
  146. What can I possibly do to express my gratitude?\n\n\
  147. I understand you have a spastic Media Lab 8-man crew team\n\
  148. that desperately needs funding for a new shell.\n\
  149. Word is, you don't even have money for uniforms\n\
  150. and have been rowing in the nude; this will not be a pretty sight at Henley.\n\
  151. I'm sure I can cough up a few bucks, at least enough\n\
  152. to help you keep the cox covered (tax-deductible contributions may\n\
  153. be sent to Crewmeister Bob Green, c/o MIT Media Lab, 20 Ames Street,\n\
  154. Cambridge, MA 02139).\n\n\
  155. Meanwhile, you will doubtless appreciate hearing about the following problem:\n\n\
  156.    <insert irritating bug here>\n\n\
  157.              rebarbitively,\n\
  158.              ";
  159. #define fcall(a)  [s performRemoteMethod:a]
  160. #define call(a,b) [s performRemoteMethod:a with:b length:strlen(b)+1]
  161.     id s = [NXApp appSpeaker];
  162.     extern NXPortFromName();
  163.  
  164.     NXPortFromName("Mail", NULL); // make sure app is launched
  165.     [s setSendPort:NXPortFromName("MailSendDemo", NULL)];
  166.  
  167.     sprintf(subj,"Comments and suggestions for ``%s'' %s",AppName,Version());
  168.     sprintf(body+strlen(body),"%s\n",execstr(w));
  169.     call("setTo:",CommentsTo);
  170.     call("setSubject:",subj);
  171.     call("setBody:",body);
  172.     
  173.     return self;
  174. }
  175.  
  176. static int init = 0;
  177.  
  178. - setScrollview:t { // staple in a ColorScroller
  179.     id s;
  180.     NXRect r;
  181.     NXColor paleYellow = NXConvertRGBToColor(1.0, 238./256., 200./256.);
  182.     scrollview = t;
  183.     [[t vertScroller] getFrame:&r];
  184.     s = [[ColorScroller alloc] initFrame:&r];
  185.     [t setVertScroller:s];
  186.     t = [t docView];
  187.     [t setBackgroundColor:paleYellow];
  188.     // fix scrollbar thumb
  189.     [t setSel:0:0];
  190.     [t replaceSel:"\n"];
  191.     [t setEditable:NO];
  192.     return self;
  193. }
  194.  
  195. - (void) load {
  196.     if (!init){
  197.         init++, [NXApp loadNibSection:"License.nib" owner:self];
  198.     _version = version;
  199.     [infobars setBackgroundColor:NX_COLORLTGRAY];
  200.     [infobars setDrawstyle:CLEAR];
  201.     [infopanel setDelegate:self];
  202.         [panel setBackgroundGray:1.];
  203.         if (!FirstUse()){ // already registered, so rearrange the panel
  204.             init++;
  205.         [quit setIcon:"registered"];
  206.         [quit setBordered:NO];
  207.         [quit setEnabled:NO];
  208.         [quit sizeToFit];
  209.         [quit moveBy:-32.:-8.];
  210.         [accept setTitle:"Okay"];
  211.         }
  212.     }
  213. }
  214.  
  215. - show:sender {
  216.     [self load];
  217.     [colorbars setDrawstyle:CLEAR];
  218.     [panel makeKeyAndOrderFront:sender];
  219.     [colorbars setDrawstyle:SWAB];
  220.     [NXApp runModalFor:panel];
  221.     return self;
  222. }
  223.  
  224. - info:sender {
  225.     [self load];
  226.     [infobars setDrawstyle:CLEAR];
  227.     [infopanel makeKeyAndOrderFront:sender];
  228.     [infobars setDrawstyle:SWAB];
  229.     return self;
  230. }
  231.  
  232. - sendReceipt { // send a receipt to indicate the user has read the license
  233.     char *ctime(), s[1024];
  234.     long time(), t = time(0);
  235.  
  236.     sprintf(s,"%s-%s-registered",AppName,Version());
  237.     if (t < 793414430 /* 1995 */) mailReceipt(s,0,ReceiptsTo);
  238.     strcpy(FirstUsed,ctime(&t));
  239.     local_stripnl(FirstUsed);
  240.     writeDefaults();
  241.     return self;
  242. }
  243.  
  244. - accept:sender {
  245.     if (init < 2){ // accept the license: rearrange panel & send receipt
  246.     id s = [Sound findSoundFor:"Stamp"];
  247.         init++;
  248.     [panel disableFlushWindow];
  249.         [quit setIcon:"registered"];
  250.         [quit setBordered:NO];
  251.         [quit setEnabled:NO];
  252.         [quit sizeToFit];
  253.     [quit setTransparent:YES];
  254.         [quit moveBy:-32.:-8.];
  255.         [accept setTitle:"Okay"];
  256.     [panel display];
  257.     [[panel reenableFlushWindow] flushWindow];
  258.     NXPing();
  259.     [quit setTransparent:NO];
  260.     [s play:self];
  261.     [quit display];
  262.     NXPing();
  263.     [self sendReceipt];
  264.     }
  265.     [NXApp stopModal];
  266.     [colorbars stopIt];
  267.     [colorbars setDrawstyle:WINKOFF];
  268.     [panel orderOut:sender];
  269.     return self;
  270. }
  271.  
  272. - quit:sender {
  273.     [colorbars stopIt];
  274.     [colorbars setDrawstyle:WINKOFF];
  275.     exit(0);
  276. }
  277.  
  278. @end
  279.